'use strict';
const db = uniCloud.database()
const swiperCollection = db.collection('swiper_data')
const newsCollection = db.collection('news-list')
const categoryCollection = db.collection('cate-list')
const knowledgeCollection = db.collection('knowledge-list')

module.exports = {
    // 获取轮播图数据
    async getSwiperData() {
      try {
        const {
          data,
          err
        } = await swiperCollection
          .limit(5) // 限制返回数量
          .get()

        if (err) {
          return {
            errCode: 1,
            errMsg: '获取轮播图失败'
          }
        }

        return {
          errCode: 0,
          errMsg: '获取成功',
          data
        }
      } catch (error) {
        return {
          errCode: 1,
          errMsg: error.message || '获取轮播图失败'
        }
      }
    },

    // 获取新闻列表
    async getNewsList(event) {
      const {
        page = 1, pageSize = 10
      } = event

      try {
        // 获取新闻列表
        const {
          data,
          err
        } = await newsCollection
          .orderBy('createTime', 'desc')
          .skip((page - 1) * pageSize)
          .limit(pageSize)
          .get()

        if (err) {
          return {
            errCode: 1,
            errMsg: '获取新闻列表失败'
          }
        }

        // 获取总数
        const {
          total
        } = await newsCollection.count()

        return {
          errCode: 0,
          errMsg: '获取成功',
          data: data.map(item => ({
            id: item._id,
            title: item.title,
            image: item.image,
            date: item.createTime,
            views: item.views || 0,
            summary: item.summary
          })),
          total
        }
      } catch (error) {
        return {
          errCode: 1,
          errMsg: error.message || '获取新闻列表失败'
        }
      }
    },

    // 获取分类数据
    async getCategoryData() {
      try {
        const {
          data,
          err
        } = await categoryCollection
          .get()

        if (err) {
          return {
            errCode: 1,
            errMsg: '获取分类数据失败'
          }
        }

        return {
          errCode: 0,
          errMsg: '获取成功',
          data
        }
      } catch (error) {
        return {
          errCode: 1,
          errMsg: error.message || '获取分类数据失败'
        }
      }
    },

    // 获取新闻详情
    async getNewsDetail(id) {
      if (!id) {
        return {
          errCode: 1,
          errMsg: '新闻ID不能为空'
        }
      }

      try {
        const db = uniCloud.database();
        const newsCollection = db.collection('news-list');

        // 获取新闻详情
        const news = await newsCollection.doc(id).get();

        if (!news.data || news.data.length === 0) {
          return {
            errCode: 1,
            errMsg: '新闻不存在'
          }
        }

        // 更新浏览量
        await newsCollection.doc(id).update({
          views: db.command.inc(1)
        });

        return {
          errCode: 0,
          errMsg: '获取成功',
          data: news.data[0]
        }
      } catch (error) {
        return {
          errCode: 1,
          errMsg: '获取新闻详情失败'
        }
      }
    },

	    // 获取知识列表数据
		async getKnowledgeList() {
			try {
			  const {
				data,
				err
			  } = await knowledgeCollection
				.get()
	  
			  if (err) {
				return {
				  errCode: 1,
				  errMsg: '获取知识列表失败'
				}
			  }
	  
			  return {
				errCode: 0,
				errMsg: '获取成功',
				data
			  }
			} catch (error) {
			  return {
				errCode: 1,
				errMsg: error.message || '获取只是列表失败'
			  }
			}
		  },

    // 获取指南详情
    async getGuideDetail(params) {
      const { id } = params;
      
      try {
        const db = uniCloud.database();
        const guideCollection = db.collection('knowledge-list');
        
        // 获取指南详情
        const guide = await guideCollection.doc(id).get();
        console.log(guide);
        if (!guide.data) {
          return {
            errCode: 1,
            errMsg: '指南不存在'
          }
        }
        
        return {
          errCode: 0,
          data: guide.data[0]
        }
      } catch (error) {
        return {
          errCode: 1,
          errMsg: error.message || '获取指南详情失败'
        }
      }
    }
}